﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Beginning.Api.Application.Command.BreedingSow;
using RearLivestock.Beginning.Api.Application.Command.Infancie;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.Echarts
{
    public class GetPigTypeEchartsHandler : IRequestHandler<GetPigTypeEchartsCommand, Result>
    {
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicInformationRepository;
        /// <summary>
        /// 猪只类型
        /// </summary>
        private readonly IBaseRepository<PigType> pigTypeRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchBreedingSowHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="basicInformationRepository"></param>
        /// <param name="pigTypeRepository"></param>
        /// <param name="logger"></param>
        public GetPigTypeEchartsHandler(IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<PigType> pigTypeRepository, ILogger<SearchBreedingSowHandler> logger)
        {
            this.basicInformationRepository = basicInformationRepository;
            this.pigTypeRepository = pigTypeRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetPigTypeEchartsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = from a in (await basicInformationRepository.GetAll())
                           join b in (await pigTypeRepository.GetAll())
                           on a.PigTypeId equals b.PigTypeId
                           group new{a,b} by new {b.PigTypeName } into c
                           select new PigCount()
                           {
                               name = c.Key.PigTypeName,
                               value = c.Count()
                           };
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                result.Msg = "获取种猪只类型分组信息成功";
            }
            catch (Exception ex)
            {
                result.Msg = "获取种猪只类型分组信息异常" + ex.Message;
                logger.LogError("获取种猪只类型分组信息异常" + ex.Message);
            }
            return result;
            //var result = new Result() { Code = (int)CommonErrorCode.Search };
            //try
            //{
            //    var list =from a in (await basicInformationRepository.GetAll())
            //                 join b in (await pigTypeRepository.GetAll())
            //                 on a.PigTypeId equals b.PigTypeId
            //                 select new EchartsBasicInformationDto()
            //                 {
            //                     BasicInforId = a.BasicInforId,
            //                     EarNumber = a.EarNumber,
            //                     Electronicearlabel = a.Electronicearlabel,
            //                     PigTypeId = a.PigTypeId,
            //                     PigTypeName = b.PigTypeName,
            //                 };
            //    if (request.PigTypeId != -1)
            //    {
            //        list = list.Where(d => d.PigTypeId == request.PigTypeId);
            //    }
            //    var res = list.GroupBy(d=>d.PigTypeName).Select(d=>new PigCount()
            //    { 
            //        name = d.Key,
            //        value = d.Count()
            //    }).ToList();
            //    result.Code = (int)CommonErrorCode.Success;
            //    result.Data = res;
            //    result.Msg = "获取种猪只类型分组信息成功";
            //}
            //catch (Exception ex)
            //{
            //    result.Msg = "获取种猪只类型分组信息异常" + ex.Message;
            //    logger.LogError("获取种猪只类型分组信息异常" + ex.Message);
            //}
            //return result;
        }
    }
}
